package com.eclab.hello.service.impl;

import com.eclab.hello.enums.ResultEnum;
import com.eclab.hello.exception.UserException;
import com.eclab.hello.model.User;
import com.eclab.hello.model.VO.UserOutputVO;
import com.eclab.hello.repository.UserRepository;
import com.eclab.hello.service.UserService;
import com.eclab.hello.utils.HelloUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 描述:
 * 用户业务实现类
 * @author yd
 * @create 2019-06-04 09:12
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;


    /**
     * 用戶登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public User login(String username, String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            throw new UserException(ResultEnum.USERNAMEANDPASSWORD_ERROR);
        }

        User user = userRepository.findUserByUsername(username);
        String inPwd = HelloUtils.MD5encode(password);
        if (user == null){
            throw new UserException(ResultEnum.USER_NOT_FOUND);
        }else if (!StringUtils.equals(inPwd,user.getPassword())){
            throw new UserException(ResultEnum.PASSWORD_ERROR);
        }

        return user;
    }

    /**
     * 用户列表
     *
     * @param pageable
     * @return
     */
    @Override
    public Page<UserOutputVO> listByPage(Pageable pageable) {
        Page<User> userPage = userRepository.findAll(pageable);
        List<User> userList = userPage.getContent();
        List<UserOutputVO> userOutputVoList = new ArrayList<>();

        userList.stream().forEach(e -> {
            UserOutputVO userOutputVo = new UserOutputVO();
            BeanUtils.copyProperties(e, userOutputVo);
            userOutputVoList.add(userOutputVo);
        });

        return new PageImpl<>(userOutputVoList, pageable, userPage.getTotalElements());
    }

    /**
     * 用户详情
     *
     * @param id
     * @return
     */
    @Override
    public User getUser(Integer id) {
        if (id == null){
            throw new UserException(ResultEnum.PARAM_ERROR);
        }
        return userRepository.getOne(id);
    }

    /**
     * 创建用户
     *
     * @param user
     * @return
     */
    @Override
    @Transactional
    public void create(User user) {
        if (StringUtils.isNotBlank(user.getUsername()) && StringUtils.isNotBlank(user.getPassword())){
            String encodePwd = HelloUtils.MD5encode(user.getPassword());
            user.setPassword(encodePwd);
            userRepository.save(user);
        }else {
            throw new UserException(ResultEnum.USERNAMEANDPASSWORD_ERROR);
        }

    }

    /**
     * 更新用户
     *
     * @param user
     * @return
     */
    @Override
    public void update(User user) {
        create(user);
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        if (id == null){
            throw new UserException(ResultEnum.PARAM_ERROR);
        }
        userRepository.deleteById(id);
    }
}
